//
//  HJVisitFriendController.m
//  Keping
//
//  Created by a on 2017/8/28.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "HJVisitFriendController.h"
#import "HJVisitFriendInfoController.h"
#import "HJVisitFriendViewCell.h"
#import "HJHeaderImgView.h"
#import "HJVisitFriendRequest.h"
#import "HJSelectDateView.h"
#import "HJVisitFriendModel.h"

@interface HJVisitFriendController ()<UITableViewDataSource,UITableViewDelegate,HJSelectDateViewDelegate>
/**
 tableView
 */
@property(nonatomic,weak)UITableView*tableView;
/**
 数据源
 */
@property(nonatomic,strong)NSMutableArray*dataAry;
/**
 服务器获取数据
 */
@property(nonatomic,copy) NSArray *visitFriends;
@end

@implementation HJVisitFriendController

-(NSMutableArray *)dataAry
{
    if (!_dataAry) {
        RelationFriendViews *friend1 = [[RelationFriendViews alloc] init];
        friend1.nickName = @"* Select a friend";
        RelationFriendViews *friend2 = [[RelationFriendViews alloc] init];
        friend2.nickName = @"* Date";
        RelationFriendViews *friend3 = [[RelationFriendViews alloc] init];
        friend3.nickName = @"* Time";
        
        _dataAry=[NSMutableArray arrayWithObjects:@[friend1],@[friend2,friend3], nil];
    }
    return _dataAry;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
    [self getData];
}
-(void)setUI
{
    
    self.navigationItem.title=@"Visit a Friend";
    UITableView*tableView=[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    tableView.delegate=self;
    tableView.dataSource=self;
    tableView.backgroundColor=[UIColor colorWithHexString:@"#f2f1f6"];
    [self.view addSubview:tableView];
    self.tableView=tableView;
    
    //设置头部
    HJHeaderImgView*headerView=[HJHeaderImgView headerImgView:KAppointmentTypeForVisit];
    tableView.tableHeaderView=headerView;
    
    //添加table尾部
    UIView*footerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, K_FactorH(80))];
    tableView.tableFooterView=footerView;
    footerView.backgroundColor=[UIColor clearColor];
    CGFloat margin =K_FactorW(20);
    UIButton*addBtn=[[UIButton alloc]initWithFrame:CGRectMake(margin, K_FactorH(30), footerView.width-2*margin, footerView.height-K_FactorH(30))];
    [footerView addSubview:addBtn];
    addBtn.layer.cornerRadius=addBtn.height/2;
    addBtn.layer.masksToBounds=YES;
    [addBtn setBackgroundColor:[UIColor colorWithHexString:@"#6853fa"] forState:UIControlStateNormal];
    [addBtn setTitle:@"Next Step" forState:UIControlStateNormal];
    addBtn.titleLabel.font=[UIFont boldSystemFontOfSize:20];
    [addBtn addTarget:self action:@selector(clickNextStep) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark 获取好友列表数据
- (void)getData {
    [super getData];
    [self showLoading];
    WEAKSELF;
    HJVisitFriendRequest *fRequest = [[HJVisitFriendRequest alloc] initWithIsBlock:@""];
    [fRequest startWithCompletionBlockWithSuccess:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf hideHUD];
        HJVisitFriendModel *model = [HJVisitFriendModel modelWithJSON:request.responseData];
        if (model.statusCode == 200) {
            weakSelf.visitFriends = model.relationFriendViews;
            [weakSelf.tableView reloadData];
        } else {
            [weakSelf showErrorText:model.msg errCode:model.statusCode];
        }
    } failure:^(__kindof KBaseRequest * _Nonnull request) {
        [weakSelf showError:request.error];
    }];
}

#pragma mark 下一步
-(void)clickNextStep
{
    NSString *relationFriendID,*date,*time;
    RelationFriendViews *friend1 = self.dataAry[0][0];
    relationFriendID = friend1.friendId;
    if (!relationFriendID) {
        ALERT(@"Please reselect a friend!");
        return;
    }
    RelationFriendViews *friend2 = self.dataAry[1][0];
    date = friend2.content;
    if (!date) {
        ALERT(@"Please reselect date!");
        return;
    }
    
    RelationFriendViews *friend3 = self.dataAry[1][1];
    time = friend3.content;
    if (!time) {
        ALERT(@"Please reselect time!");
        return;
    }
    HJVisitFriendInfoController *vc = [HJVisitFriendInfoController new];
    vc.preType = PreAppointmentTypeForVisit;
    vc.relationFriendID = relationFriendID;
    vc.date = date;
    vc.time = time;
    [self.navigationController pushViewController:vc animated:YES];
}

- (NSArray *)visitFriends {
    if (!_visitFriends) {
        _visitFriends = [NSArray new];
    }
    return _visitFriends;
}

#pragma mark --UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataAry.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataAry[section] count];
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HJVisitFriendViewCell*cell=[HJVisitFriendViewCell cellWithTableView:tableView];
    RelationFriendViews *model = self.dataAry[indexPath.section][indexPath.row];
    cell.title = model.nickName;
    cell.content = model.content;
    cell.isEdit = NO;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        HJVisitFriendViewCell*cell=[tableView cellForRowAtIndexPath:indexPath];
        cell.isSelect=!cell.isSelect;
        if (cell.isSelect) {
            //插入好友数据
            NSMutableArray*ary=[self.dataAry[0] mutableCopy];
            [ary addObjectsFromArray:self.visitFriends];
            [self.dataAry replaceObjectAtIndex:0 withObject:ary];
            //刷新界面
            [self.tableView reloadData];
        }else
        {
            //删除好友数据
            NSMutableArray*ary = [self.dataAry[0] mutableCopy];
            [ary removeObjectsInArray:self.visitFriends];
            [self.dataAry replaceObjectAtIndex:0 withObject:ary];
            //刷新界面
            [self.tableView reloadData];
            
        }
    }else if (indexPath.section == 1)
    {
        //弹出时间选择器
        if (indexPath.row == 0) {
            //弹出年月日
            [self creatDatePicker:UIDatePickerModeDate];
        }else if (indexPath.row == 1)
        {
            //弹出时分秒
            [self creatDatePicker:UIDatePickerModeTime];
        }
    }else
    {
        //选择好友
        RelationFriendViews *friend = self.dataAry[indexPath.section][indexPath.row];
        RelationFriendViews *friendTmp = self.dataAry[0][0];
        friendTmp.content = friend.nickName;
        friendTmp.friendId = friend.friendId;
        [self.dataAry replaceObjectAtIndex:0 withObject:@[friendTmp]];
        
        [self.tableView reloadSection:0 withRowAnimation:UITableViewRowAnimationNone];
    }
}
-(void)creatDatePicker:(UIDatePickerMode)dateModel
{
    HJSelectDateView*dateView=[[HJSelectDateView alloc]initWithFrame:CGRectMake(0, 0, App_Frame_Width, APP_Frame_Height)];
    dateView.delegate=self;
    dateView.DateModel=dateModel;
    [self.navigationController.view addSubview:dateView];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return K_FactorH(60);
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return K_FactorH(10);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1f;
}

#pragma mark -- HJSelectDateViewDelegate
-(void)selectDateView:(HJSelectDateView*)selectDateView clickSelectDate:(NSString*)dateStr indexPath:(NSIndexPath*)indexPath {
    NSMutableArray *arr = [self.dataAry[1] mutableCopy];
    if (selectDateView.DateModel == UIDatePickerModeDate) {
        RelationFriendViews *friend = [arr objectAtIndex:0];
        friend.content = dateStr;
        [arr replaceObjectAtIndex:0 withObject:friend];
    } else {
        RelationFriendViews *friend = [arr objectAtIndex:1];
        friend.content = dateStr;
        [arr replaceObjectAtIndex:1 withObject:friend];
    }
    [self.dataAry replaceObjectAtIndex:1 withObject:arr];
    [self.tableView reloadSection:1 withRowAnimation:UITableViewRowAnimationNone];;
}

@end
